package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装为Request对象（根据InputSteam输入流封装）
 */
public class Request {

    private String method; // 请求方式，比如GET/POST
    private String requestUrl;  // 例如 /,/index.html
    private String hostName;
    private String port;

    private InputStream inputStream;  // 输入流，其他属性从输入流中解析出来


    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public Request() {
    }


    // 构造器，输入流传入
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        inputStream.read(bytes);

        String inputStr = new String(bytes);


        String[] allLines = inputStr.split("\\n");

        String line1 = allLines[0];
        String[] line1StrArr = line1.split(" ");
        String method0 = line1StrArr[0];
        String subUrl = line1StrArr[1];

        String line2 = allLines[1];
        String hostAndPort = line2.substring(line2.indexOf(":") + 1).trim();
        String[] hostAndPortArr = hostAndPort.split(":");

        this.method = method0;
        this.hostName = hostAndPortArr[0];
        this.port = hostAndPortArr[1];
        this.requestUrl = subUrl;

        System.out.println("=====>>method:" + method0);
        System.out.println("=====>>hostName:" + hostName);
        System.out.println("=====>>port:" + port);
        System.out.println("=====>>url:" + requestUrl);


    }
}
